/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

/**
globals (should not be needed anymore)
*/
g_channel_list = null;
g_video_player = null;
g_annotation_browser = null;
g_progress_bar = null;
g_counter = null;
g_annotation_viewer = null;
g_annotation_maker = null;
g_box = null;
ag_box = null;
g_player_overlay = null;

/**
get the contents of the site in json format
use the lists of annotations,channels and movies as soon as the site is loaded
*/
$(document).ready(function() {
	
	NameToColor("bla")
	
	path = document.location.pathname;
	subs = path.split("/");
	mode = subs[1] //we need to know whether we are in search (playlist) or movie mode
	
	if((mode == "movie") || mode == "search")
	{
		json_data_url =  document.location.href + (document.location.search==""? "?" : "&" ) + "format=json"

		$.ajax({
			type: "GET",
			url: json_data_url,
			dataType: "json",
			async:false,
			success: function(data)
			{
				if(data.success == true) //
				{
					onReceivedData(data.channels, data.movies,  data.annotations, mode);
				}
			}
		});
	}
});

/**
add appropraite interactive elements (like the movie player and the annotation editor) as soon as we
receive the lists of channel, annotations and movies
*/
function onReceivedData(channels, movies, annotations, mode)
{
		if(channels.length == 0) return; //[Mvh] alert("no channels!!")
		if(movies.length == 0) return;//[MvH] alert("no movies!!")

		//put the channels in a global list
		g_channel_list = new Array();
		for(var i=0;i<channels.length;i++)
		{
			channel = channels[i];
			channel.__proto__ = new Channel(); //now we can call its functions
			channel.annotations = new Array();
			g_channel_list.push(channel);
		}

		//make movies array handy searchable
		movies.__proto__ = new Array();
		for(var i=0;i<movies.length;i++)
		{
			movie = movies[i];
			movie.__proto__ = new Movie();
		}

		//add the annotations to appopriate channels
		for(var i = 0; i<annotations.length;i++)
		{
			annotation = annotations[i];
			annotation.__proto__ = new Annotation();
			annotation.movie = movies.findObj("id",annotation.movieId);
			annotation.time = parseInt(annotation.time) //should not be necesairy (server send string)
			annotation.duration = parseInt(annotation.duration) //should not be necesairy
			
			channel = g_channel_list.findObj("id",annotation.channelId);
			annotation.channel = channel
			
			if(channel != null)
			{
				try
				{
					channel.annotations.push(annotation);
				}
				catch(ex)
				{
					alert(ex+" "+annotation.channelId+" "+channel);
				}
			}
			else
			{
				//throw "received annotation for non-existing channel " + annotation.id
			}
		}

		//make own array functions work on this
		annotations.__proto__ = new Array();

		if(mode == "search")
		{
			playlist = annotations;
		}
		else
		{
			playlist = new Array();
			item = new Annotation();
			item.movie = movies[0];
			item.movieId = movies[0].id;
			item.time = 0; //begin
			item.duration = movies[0].duration;
			playlist.push(item);
		}

		player_container = document.getElementById("atPlayer")
		if(player_container)
		{
			$(player_container).empty()

			//add the player itself (just video without the controls)
			g_video_player = new VideoPlayer(player_container,playlist)

			//add overlay controller for all stuff that is placed on top of the video
			g_player_overlay = new PlayerOverlay(player_container)

			g_time = new TimeViewer(g_player_overlay.getTopArea())
			g_playbutton = new PlayButton(g_player_overlay.getBottomArea())
			g_progress_bar = new ProgressBar(g_player_overlay.getBottomArea(),playlist)

			g_annotation_viewer = new AnnotationViewer(g_player_overlay.getMiddleArea(),annotations) //annotations
		}

		maker_container = document.getElementById("atAnnotationMaker")

		if(maker_container)
		{
			$(maker_container).empty()
			g_annotation_maker = new AnnotationMaker(maker_container,channels,movies[0])
		}

		browser_container = document.getElementById("atAnnotationBrowser")
		if(browser_container)
		{
			$(browser_container).empty()
			editable = (maker_container != null)

			g_annotation_browser = new AnnotationBrowser(g_channel_list,browser_container,editable);
		}

		info_container = document.getElementById("atAnnotationInfo")
		if(info_container)
		{
			$(info_container).empty()
			new AnnotationInfo(info_container)
		}
}
